cmake_minimum_required(VERSION 3.8)
project(multi_launch_demo)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(std_msgs REQUIRED)
find_package(std_srvs REQUIRED)
find_package(geometry_msgs REQUIRED)

# 创建发布者节点
add_executable(publisher_node src/publisher_node.cpp)
ament_target_dependencies(publisher_node rclcpp std_msgs)

# 创建订阅者节点
add_executable(subscriber_node src/subscriber_node.cpp)
ament_target_dependencies(subscriber_node rclcpp std_msgs)

# 创建服务端节点
add_executable(service_server_node src/service_server_node.cpp)
ament_target_dependencies(service_server_node rclcpp std_msgs std_srvs)

# 创建客户端节点
add_executable(service_client_node src/service_client_node.cpp)
ament_target_dependencies(service_client_node rclcpp std_msgs std_srvs)

# 创建参数节点
add_executable(param_node src/param_node.cpp)
ament_target_dependencies(param_node rclcpp std_msgs)

# 创建组件库（共享库）
add_library(component_lib SHARED
  src/component_publisher.cpp
  src/component_subscriber.cpp
  src/component_param.cpp
  src/component_processor.cpp
  src/component_aggregator.cpp
  src/component_monitor.cpp
)
ament_target_dependencies(component_lib
  rclcpp
  rclcpp_components
  std_msgs
)

# 注册组件
rclcpp_components_register_node(component_lib
  PLUGIN "multi_launch_demo::ComponentPublisher"
  EXECUTABLE component_publisher
)
rclcpp_components_register_node(component_lib
  PLUGIN "multi_launch_demo::ComponentSubscriber"
  EXECUTABLE component_subscriber
)
rclcpp_components_register_node(component_lib
  PLUGIN "multi_launch_demo::ComponentParam"
  EXECUTABLE component_param
)
rclcpp_components_register_node(component_lib
  PLUGIN "multi_launch_demo::ComponentProcessor"
  EXECUTABLE component_processor
)
rclcpp_components_register_node(component_lib
  PLUGIN "multi_launch_demo::ComponentAggregator"
  EXECUTABLE component_aggregator
)
rclcpp_components_register_node(component_lib
  PLUGIN "multi_launch_demo::ComponentMonitor"
  EXECUTABLE component_monitor
)

# 安装可执行文件
install(TARGETS
  publisher_node
  subscriber_node
  service_server_node
  service_client_node
  param_node
  DESTINATION lib/${PROJECT_NAME}
)

# 安装组件库
install(TARGETS
  component_lib
  DESTINATION lib
)

# 安装launch文件
install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)

# 安装配置文件
install(DIRECTORY
  config
  DESTINATION share/${PROJECT_NAME}/
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()
